package com.flysh.host.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.flysh.host.model.User;
import com.flysh.host.util.DaoUtil;

public class UserDaoImpl implements UserDao{

	public User login(User user) {
		User u = null;
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		ResultSet rs = null;
		String sql = "select * from t_user where NAME=? and PWD=?";
		try {
			 ps = conn.prepareStatement(sql);
			 ps.setObject(1, user.getUsername());
			 ps.setObject(2, user.getPassword());
			 rs = ps.executeQuery();
			 if(rs.next()){
				 u = new User();
				 Long id = rs.getLong("ID");
				 u.setId(id);
				 u.setUsername(rs.getString("NAME"));
				 u.setPassword(rs.getString("PWD"));
				 u.setEmail(rs.getString("E_MAIL"));
			 }
			 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, rs);
		}
		return u;
	}

	@Override
	public int remove(Long id) {
		int num=0;
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		String sql = "delete from T_USER where id=?";
		 try {
			ps = conn.prepareStatement(sql);
			ps.setObject(1, id);
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, null);
		}
		return num;
		
	}

	@Override
	public int update(Long id, User newUser) {
		int num=0;
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		String sql = "update T_USER set NAME=?,PWD=? where ID=?";
		 try {
			ps = conn.prepareStatement(sql);
			ps.setObject(1, newUser.getUsername());
			ps.setObject(2, newUser.getPassword());
			ps.setObject(3, id);
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, null);
		}
		return num;
		
	}

	public User getUser(Long id) {
		User u = null;
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		ResultSet rs = null;
		String sql = "select * from t_user where ID=?";
		try {
			 ps = conn.prepareStatement(sql);
			 ps.setObject(1, id);
			 rs = ps.executeQuery();
			 if(rs.next()){
				 u = new User();
				 u.setId(rs.getLong("ID"));
				 u.setUsername(rs.getString("NAME"));
				 u.setPassword(rs.getString("PWD"));
				 u.setEmail(rs.getString("E_MAIL"));
			 }
			 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, rs);
		}
		return u;
	}

	@Override
	public List<User> getAllUser() {
		List<User> list = new ArrayList<User>();
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		ResultSet rs = null;
		String sql = "select * from t_user";
		try {
			 ps = conn.prepareStatement(sql);
			 rs = ps.executeQuery();
			 if(rs.next()){
				 User u = new User();
				 u.setId(rs.getLong("ID"));
				 u.setUsername(rs.getString("NAME"));
				 u.setPassword(rs.getString("PWD"));
				 u.setEmail(rs.getString("E_MAIL"));
				 list.add(u);
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int add(User user) {
		int num=0;
		Connection conn = DaoUtil.getConnection();
		PreparedStatement ps  = null;
		String sql = "insert into T_USER(NAME,PWD,TRUE_NAME,E_MAIL) values(?,?,?,?)";
		 try {
			ps = conn.prepareStatement(sql);
			ps.setObject(1, user.getUsername());
			ps.setObject(2, user.getPassword());
			ps.setObject(3, user.getTruename());
			ps.setObject(4, user.getEmail());
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DaoUtil.close(conn, ps, null);
		}
		return num;
	}

	

}
